package com.lee.pullrefresh.http;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.Random;
import java.util.regex.Pattern;

import android.text.format.DateFormat;
import android.util.Log;

public final class DateTimeUtils {

	/**
	 * ��ȡ����ʱ��
	 * 
	 * @return ����ʱ������ yyyy-MM-dd HH:mm:ss
	 */
	public static Date getNowDate() {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateString = formatter.format(currentTime);
		ParsePosition pos = new ParsePosition(0);
		Date currentTime_2 = formatter.parse(dateString, pos);
		return currentTime_2;
	}

	public static String getNowDateString() {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return formatter.format(currentTime);
	}

	/**
	 * ��ȡ����ʱ��
	 * 
	 * @return���ض�ʱ���ʽ yyyy-MM-dd
	 */
	public static Date getNowDateShort() {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String dateString = formatter.format(currentTime);
		ParsePosition pos = new ParsePosition(0);
		Date currentTime_2 = formatter.parse(dateString, pos);
		return currentTime_2;
	}

	/**
	 * ��ȡ����ʱ��
	 * 
	 * @return�����ַ�����ʽ yyyy-MM-dd HH:mm:ss
	 */
	public static String getStringDate() {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
		String dateString = formatter.format(currentTime);
		return dateString;
	}

	/**
	 * ��ȡ����ʱ��
	 * 
	 * @return ���ض�ʱ���ַ�����ʽyyyy-MM-dd
	 */
	public static String getStringDateShort() {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String dateString = formatter.format(currentTime);
		return dateString;
	}

	/**
	 * ��ȡ��������1��
	 * 
	 * @return ���ض�ʱ���ַ�����ʽyyyy-MM-01
	 */
	public static String getStringDateYM1() {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-01");
		String dateString = formatter.format(currentTime);
		return dateString;
	}

	/**
	 * ��ȡʱ�� Сʱ:��;�� HH:mm:ss
	 * 
	 * @return
	 */
	public static String getTimeShort() {
		SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
		Date currentTime = new Date();
		String dateString = formatter.format(currentTime);
		return dateString;
	}

	/** * ����ʱ���ʽʱ��ת��Ϊ�ַ��� yyyy-MM-dd HH:mm:ss * * @param dateDate * @return */
	public static String dateToStrLong(java.util.Date dateDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateString = formatter.format(dateDate);
		return dateString;
	}

	/**
	 * ����ʱ���ʽʱ��ת��Ϊ�ַ��� yyyy-MM-dd
	 * 
	 * @param dateDate
	 * @param k
	 * @return
	 */
	public static String dateToStr(java.util.Date dateDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String dateString = formatter.format(dateDate);
		return dateString;
	}

	/**
	 * ����ʱ���ʽ�ַ���ת��Ϊʱ�� yyyy-MM-dd
	 * 
	 * @param strDate
	 * @return
	 */
	public static Date strToDate(String strDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		ParsePosition pos = new ParsePosition(0);
		Date strtodate = formatter.parse(strDate, pos);
		return strtodate;
	}
	


	/**
	 * ����ʱ���ʽ�ַ���ת��Ϊʱ�� yyyy-MM-dd HH:mm:ss
	 * 
	 * @param strDate
	 * @return
	 */
	public static Date strToDateLong(String strDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		ParsePosition pos = new ParsePosition(0);
		Date strtodate = formatter.parse(strDate, pos);
		return strtodate;
	}

	/**
	 * ����ʱ���ʽ�ַ���ת��Ϊʱ�� yyyy-MM-dd HH:mm
	 * 
	 * @param strDate
	 * @return
	 */
	public static Date strToDateLongNs(String strDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		ParsePosition pos = new ParsePosition(0);
		Date strtodate = formatter.parse(strDate, pos);
		return strtodate;
	}


	/**
	 * �õ�����ʱ��
	 * 
	 * @return
	 */
	public static Date getNow() {
		Date currentTime = new Date();
		return currentTime;
	}

	/**
	 * ��ȡһ�����е����һ��
	 * 
	 * @param day
	 * @return
	 */
	public static Date getLastDate(long day) {
		Date date = new Date();
		long date_3_hm = date.getTime() - 3600000 * 34 * day;
		Date date_3_hm_date = new Date(date_3_hm);
		return date_3_hm_date;
	}

	/**
	 * �õ�����ʱ��
	 * 
	 * @return �ַ��� yyyyMMdd HHmmss
	 */
	public static String getStringToday() {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HHmmss");
		String dateString = formatter.format(currentTime);
		return dateString;
	}

	/**
	 * �õ�����Сʱ
	 */
	public static String getHour() {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateString = formatter.format(currentTime);
		String hour;
		hour = dateString.substring(11, 13);
		return hour;
	}

	/**
	 * �õ����ڷ���
	 * 
	 * @return
	 */
	public static String getTime() {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateString = formatter.format(currentTime);
		String min;
		min = dateString.substring(14, 16);
		return min;
	}

	/**
	 * �����û������ʱ���ʾ��ʽ�����ص�ǰʱ��ĸ�ʽ �����yyyyMMdd��ע����ĸy���ܴ�д��
	 * 
	 * @param sformat
	 *            yyyyMMddhhmmss
	 * @return
	 */
	public static String getUserDate(String sformat) {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy��MM��dd��");
		String dateString = formatter.format(currentTime);
		return dateString;
	}



	/**
	 * ����Сʱʱ���Ĳ�ֵ,���뱣֤����ʱ�䶼��"HH:MM"�ĸ�ʽ�������ַ��͵ķ���
	 */
	public static String getTwoHour(String st1, String st2) {
		String[] kk = null;
		String[] jj = null;
		kk = st1.split(":");
		jj = st2.split(":");
		if (Integer.parseInt(kk[0]) < Integer.parseInt(jj[0]))
			return "0";
		else {
			double y = Double.parseDouble(kk[0]) + Double.parseDouble(kk[1])
					/ 60;
			double u = Double.parseDouble(jj[0]) + Double.parseDouble(jj[1])
					/ 60;
			if ((y - u) > 0)
				return y - u + "";
			else
				return "0";
		}
	}

	/**
	 * �õ��������ڼ�ļ������
	 */
	public static String getTwoDay(String sj1, String sj2) {
		SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
		long day = 0;
		try {

			java.util.Date date = myFormatter.parse(sj1);
			java.util.Date mydate = myFormatter.parse(sj2);
			day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
		} catch (Exception e) {
			return "";
		}
		return day + "";
	}

	public static long timeDiffByMinute(String date1, String date2) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			java.util.Date d1 = formatter.parse(date1);
			java.util.Date d2 = formatter.parse(date2);
			return (d1.getTime() - d2.getTime()) / (60 * 1000);
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return 0;
	}

	public static int compareDate(String date1, String date2) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			java.util.Date d1 = formatter.parse(date1);
			java.util.Date d2 = formatter.parse(date2);
			if (d1.getTime() > d2.getTime()) {
				return 1;
			} else if (d1.getTime() < d2.getTime()) {
				return -1;
			} else {
				return 0;
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return 0;
	}

	/**
	 * �õ��������ڼ�ļ��������Сʱ������ yyyy-MM-dd HH:mm
	 */
	public static String getTwoDayNs(String sj1, String sj2) {
		SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		long day = 0;

		try {
			java.util.Date date = myFormatter.parse(sj1);
			java.util.Date mydate = myFormatter.parse(sj2);
			day = (date.getTime() - mydate.getTime() / (24 * 60 * 60 * 1000));

		} catch (Exception e) {
			return "";
		}
		return day + "";
	}

	/**
	 * ʱ��ǰ�ƻ���Ʒ���,����JJ��ʾ����.
	 */
	public static String getPreTime(String sj1, String jj) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String mydate1 = "";
		try {
			Date date1 = format.parse(sj1);
			long Time = (date1.getTime() / 1000) + Integer.parseInt(jj) * 60;
			date1.setTime(Time * 1000);
			mydate1 = format.format(date1);
		} catch (Exception e) {
		}
		return mydate1;
	}

	/**
	 * �õ�һ��ʱ���Ӻ��ǰ�Ƽ����ʱ��,nowdateΪʱ��,delayΪǰ�ƻ���ӵ�����
	 */
	public static String getNextDay(String nowdate, String delay) {
		try {
			SimpleDateFormat format = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");
			String mdate = "";
			Date d = strToDateLongNs(nowdate);
			long myTime = (d.getTime() / 1000) + Integer.parseInt(delay) * 24
					* 60 * 60;
			d.setTime(myTime * 1000);
			mdate = format.format(d);
			System.out.println("sssssssssssssssss" + mdate);
			return mdate;
		} catch (Exception e) {
			return "";
		}
	}
	/**
	 * �����û���������ڵõ�  �����еĺ���
	 * @param date
	 * @return
	 */
	public static String getUserDay(String date){
		String day = null;
		int mday = 0;
		Calendar days = new GregorianCalendar();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date mDate = df.parse(date);
			days.setTime(mDate);
			mday = days.get(Calendar.DAY_OF_MONTH);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (mday < 10) {
			day = "0"+mday;
		}else {
			day = mday+"";
		}
		return day;
	}
	/**
	 * �����û���������ڵõ�YYYY-MM
	 * @param date
	 * @return
	 */
	public static String getUserYm(String date){
		String mdate = null;
		int dY = 0;
		int dM = 0;
		Calendar days = new GregorianCalendar();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date mDate = df.parse(date);
			days.setTime(mDate);
			dY = days.get(Calendar.YEAR); 
			dM = days.get(Calendar.MONTH)+1;
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		mdate = dY+"��"+dM+"��";
		return mdate;
	}

	/**
	 * �õ�ĳ��ʱ�������ʱ������꣬�£���
	 * 
	 * @param date
	 *            ��ʱ��ֻ�ַ���
	 * @return
	 */
	public static String getYearMonthDay(String date) {
		String age = null;
		Calendar days = new GregorianCalendar();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date daystart = df.parse(date);
			days.setTime(daystart);
			
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Calendar mCalendar = Calendar.getInstance();
		int day = mCalendar.get(Calendar.DAY_OF_MONTH)- days.get(Calendar.DAY_OF_MONTH);
		int month = mCalendar.get(Calendar.MONTH) - days.get(Calendar.MONTH);
		int year = mCalendar.get(Calendar.YEAR) - days.get(Calendar.YEAR);
		// ���ռ���ԭ����day�����������month�裻Ȼ��month�����������year�裻���year�����
		if (day < 0) {
			month -= 1;
			mCalendar.add(Calendar.MONTH, -1);// �õ���һ���£������õ��ϸ��µ�������
			day = day + mCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);
		}
		if (month < 0) {
			month = (month + 12) % 12;
			year--;
		}
		// ���ֿ����ԣ� �ֱ� ���꼸���¼��죬�����¼��죬����
		if (year == 0 && month == 0) {
			age = day + "��";
		} else if (year == 0 && month != 0) {
			age = month + "����" + day + "��";
		} else if (year != 0 && month != 0) {
			age = year + "��" + month + "����" + day + "��";
		}
		return age;
	}

	/**
	 * �ж��Ƿ�����
	 * 
	 * @param ddate
	 * @return
	 */
	public static boolean isLeapYear(String ddate) {

		/**
		 * ��ϸ��ƣ� 1.��400���������꣬���� 2.���ܱ�4������������ 3.�ܱ�4����ͬʱ���ܱ�100������������
		 * 3.�ܱ�4����ͬʱ�ܱ�100������������
		 */
		Date d = strToDate(ddate);
		GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
		gc.setTime(d);
		int year = gc.get(Calendar.YEAR);
		if ((year % 400) == 0)
			return true;
		else if ((year % 4) == 0) {
			if ((year % 100) == 0)
				return false;
			else
				return true;
		} else
			return false;
	}

	/**
	 * ��������ʱ���ʽ 26 Apr 2006
	 * 
	 * @param str
	 * @return
	 */
	public static String getEDate(String str) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		ParsePosition pos = new ParsePosition(0);
		Date strtodate = formatter.parse(str, pos);
		String j = strtodate.toString();
		String[] k = j.split(" ");
		return k[2] + k[1].toUpperCase() + k[5].substring(2, 4);
	}

	/**
	 * ��ȡһ���µ����һ��
	 * 
	 * @param dat
	 * @return
	 */
	public static String getEndDateOfMonth(String dat) {// yyyy-MM-dd
		String str = dat.substring(0, 8);
		String month = dat.substring(5, 7);
		int mon = Integer.parseInt(month);
		if (mon == 1 || mon == 3 || mon == 5 || mon == 7 || mon == 8
				|| mon == 10 || mon == 12) {
			str += "31";
		} else if (mon == 4 || mon == 6 || mon == 9 || mon == 11) {
			str += "30";
		} else {
			if (isLeapYear(dat)) {
				str += "29";
			} else {
				str += "28";
			}
		}
		return str;
	}

	/**
	 * �ж϶���ʱ���Ƿ���ͬһ����
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static boolean isSameWeekDates(Date date1, Date date2) {
		Calendar cal1 = Calendar.getInstance();
		Calendar cal2 = Calendar.getInstance();
		cal1.setTime(date1);
		cal2.setTime(date2);
		int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
		if (0 == subYear) {
			if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2
					.get(Calendar.WEEK_OF_YEAR))
				return true;
		} else if (1 == subYear && 11 == cal2.get(Calendar.MONTH)) {
			// ���12�µ����һ�ܺ�������һ�ܵĻ������һ�ܼ���������ĵ�һ��
			if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2
					.get(Calendar.WEEK_OF_YEAR))
				return true;
		} else if (-1 == subYear && 11 == cal1.get(Calendar.MONTH)) {
			if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2
					.get(Calendar.WEEK_OF_YEAR))
				return true;
		}
		return false;
	}

	/**
	 * ����������,���õ���ǰʱ�����ڵ�����ǵڼ���
	 * 
	 * @return
	 */
	public static String getSeqWeek() {
		Calendar c = Calendar.getInstance(Locale.CHINA);
		String week = Integer.toString(c.get(Calendar.WEEK_OF_YEAR));
		if (week.length() == 1)
			week = "0" + week;
		String year = Integer.toString(c.get(Calendar.YEAR));
		return year + week;
	}

	/**
	 * ���һ���������ڵ��ܵ����ڼ������ڣ���Ҫ�ҳ�2002��2��3�������ܵ�����һ�Ǽ���
	 * 
	 * @param sdate
	 * @param num
	 * @return
	 */
	public static String getWeek(String sdate, String num) {
		// ��ת��Ϊʱ��
		Date dd = DateTimeUtils.strToDate(sdate);
		Calendar c = Calendar.getInstance();
		c.setTime(dd);
		if (num.equals("1")) // ��������һ���ڵ�����
			c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		else if (num.equals("2")) // �������ڶ����ڵ�����
			c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
		else if (num.equals("3")) // �������������ڵ�����
			c.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
		else if (num.equals("4")) // �������������ڵ�����
			c.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
		else if (num.equals("5")) // �������������ڵ�����
			c.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
		else if (num.equals("6")) // �������������ڵ�����
			c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
		else if (num.equals("0")) // �������������ڵ�����
			c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
		return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
	}

	/**
	 * ����һ�����ڣ����������ڼ����ַ���
	 * 
	 * @param sdate
	 * @return
	 */
	public static String getWeek(String sdate) {
		// ��ת��Ϊʱ��
		Date date = DateTimeUtils.strToDate(sdate);
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		// int hour=c.get(Calendar.DAY_OF_WEEK);
		// hour�д�ľ������ڼ��ˣ��䷶Χ 1~7
		// 1=������ 7=����������������
		return new SimpleDateFormat("EEEE").format(c.getTime());
	}

	public static String getWeekStr(String sdate) {
		String str = "";
		str = DateTimeUtils.getWeek(sdate);
		if ("1".equals(str)) {
			str = "Sunday";
		} else if ("2".equals(str)) {
			str = "Monday";
		} else if ("3".equals(str)) {
			str = "Tuesday";
		} else if ("4".equals(str)) {
			str = "Wednesday";
		} else if ("5".equals(str)) {
			str = "Thursday";
		} else if ("6".equals(str)) {
			str = "Friday";
		} else if ("7".equals(str)) {
			str = "Saturday";
		}
		return str;
	}

	/**
	 * ����ʱ��֮�������
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static long getDays(String date1, String date2) {
		if (date1 == null || date1.equals(""))
			return 0;
		if (date2 == null || date2.equals(""))
			return 0;
		// ת��Ϊ��׼ʱ��
		SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
		java.util.Date date = null;
		java.util.Date mydate = null;
		try {
			date = myFormatter.parse(date1);
			mydate = myFormatter.parse(date2);
		} catch (Exception e) {
		}
		long day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
		return day;
	}

	/**
	 * �γ����µ����� �� ���ݴ����һ��ʱ�䷵��һ���ṹ ������ ����һ ���ڶ� ������ ������ ������ ������ �����ǵ��µĸ���ʱ��
	 * �˺������ظ�������һ�����������ڵ�����
	 * 
	 * @param sdate
	 * @return
	 */
	public static String getNowMonth(String sdate) {
		// ȡ��ʱ�������µ�һ��
		sdate = sdate.substring(0, 8) + "01";

		// �õ�����µ�1�������ڼ�
		Date date = DateTimeUtils.strToDate(sdate);
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int u = c.get(Calendar.DAY_OF_WEEK);
		String newday = DateTimeUtils.getNextDay(sdate, (1 - u) + "");
		return newday;
	}

	/**
	 * ȡ�����ݿ����� ���ɸ�ʽΪyyyymmddhhmmss+kλ�����
	 * 
	 * @param k
	 *            ��ʾ��ȡ��λ������������Լ���
	 */

	public static String getNo(int k) {

		return getUserDate("yyyyMMddhhmmss") + getRandom(k);
	}

	/**
	 * ����һ�������
	 * 
	 * @param i
	 * @return
	 */
	public static String getRandom(int i) {
		Random jjj = new Random();
		// int suiJiShu = jjj.nextInt(9);
		if (i == 0)
			return "";
		String jj = "";
		for (int k = 0; k < i; k++) {
			jj = jj + jjj.nextInt(9);
		}
		return jj;
	}

	/***************************************************************************
	 * //nd=1��ʾ���ص�ֵ�а������ //yf=1��ʾ���ص�ֵ�а����·� //rq=1��ʾ���ص�ֵ�а������� //format��ʾ���صĸ�ʽ 1
	 * �����������ķ��� 2 �Ժ���-���� // 3 ��б��/���� 4 ����д��������������ʽ���� // 5 �Ե��.����
	 **************************************************************************/

	/*
	 * public static String getStringDateMonth(String sdate, String nd, String
	 * yf, String rq, String format) { Date currentTime = new Date();
	 * SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); String
	 * dateString = formatter.format(currentTime); String s_nd =
	 * dateString.substring(0, 4); // ��� String s_yf = dateString.substring(5,
	 * 7); // �·� String s_rq = dateString.substring(8, 10); // ���� String sreturn
	 * = ""; roc.util.MyChar mc = new roc.util.MyChar(); if (sdate == null ||
	 * sdate.equals("") || !mc.Isdate(sdate)) { // �����ֵ��� if (nd.equals("1")) {
	 * sreturn = s_nd; // �������� if (format.equals("1")) sreturn = sreturn + "��";
	 * else if (format.equals("2")) sreturn = sreturn + "-"; else if
	 * (format.equals("3")) sreturn = sreturn + "/"; else if
	 * (format.equals("5")) sreturn = sreturn + "."; } // �����·� if
	 * (yf.equals("1")) { sreturn = sreturn + s_yf; if (format.equals("1"))
	 * sreturn = sreturn + "��"; else if (format.equals("2")) sreturn = sreturn +
	 * "-"; else if (format.equals("3")) sreturn = sreturn + "/"; else if
	 * (format.equals("5")) sreturn = sreturn + "."; } // �������� if
	 * (rq.equals("1")) { sreturn = sreturn + s_rq; if (format.equals("1"))
	 * sreturn = sreturn + "��"; } } else { // ���ǿ�ֵ��Ҳ��һ���Ϸ�������ֵ�����Ƚ���ת��Ϊ��׼��ʱ���ʽ
	 * sdate = roc.util.RocDate.getOKDate(sdate); s_nd = sdate.substring(0, 4);
	 * // ��� s_yf = sdate.substring(5, 7); // �·� s_rq = sdate.substring(8, 10);
	 * // ���� if (nd.equals("1")) { sreturn = s_nd; // �������� if
	 * (format.equals("1")) sreturn = sreturn + "��"; else if
	 * (format.equals("2")) sreturn = sreturn + "-"; else if
	 * (format.equals("3")) sreturn = sreturn + "/"; else if
	 * (format.equals("5")) sreturn = sreturn + "."; } // �����·� if
	 * (yf.equals("1")) { sreturn = sreturn + s_yf; if (format.equals("1"))
	 * sreturn = sreturn + "��"; else if (format.equals("2")) sreturn = sreturn +
	 * "-"; else if (format.equals("3")) sreturn = sreturn + "/"; else if
	 * (format.equals("5")) sreturn = sreturn + "."; } // �������� if
	 * (rq.equals("1")) { sreturn = sreturn + s_rq; if (format.equals("1"))
	 * sreturn = sreturn + "��"; } } return sreturn; }
	 */

	/**
	 * 
	 * @param Ҫת���ĺ�����
	 * @return �ú�����ת��Ϊ * days * hours * minutes * seconds ��ĸ�ʽ
	 * @author fy.zhang
	 */
	public static String formatDuring(long mss) {
		long days = mss / (1000 * 60 * 60 * 24);
		long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
		long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);
		long seconds = (mss % (1000 * 60)) / 1000;
		return days + " �� " + hours + " Сʱ " + minutes + " ���� " + seconds
				+ " �� ";
	}

	/**
	 * 
	 * @param begin
	 *            ʱ��εĿ�ʼ
	 * @param end
	 *            ʱ��εĽ���
	 * @return ���������Date��������֮���ʱ������* days * hours * minutes * seconds�ĸ�ʽչʾ
	 * @author fy.zhang
	 */
	public static String formatDuring(Date begin, Date end) {
		return formatDuring(end.getTime() - begin.getTime());
	}
	
	/**
	 * �õ�2������֮�������·ݺ�����
	 * 
	 * @param date
	 *            ��ʱ��ֻ�ַ���
	 * @return
	 */
	public static Double getMonthDay(String date1,String date2) {
		Double age = null;
		Calendar days = new GregorianCalendar();
		Calendar days1 = new GregorianCalendar();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date day1 = df.parse(date1);
			Date day2 = df.parse(date2);
			days.setTime(day1);
			days1.setTime(day2);
			
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		int day = days.get(Calendar.DAY_OF_MONTH) - days1.get(Calendar.DAY_OF_MONTH);
		
		int month = days.get(Calendar.MONTH) - days1.get(Calendar.MONTH);
		int year = days.get(Calendar.YEAR) - days1.get(Calendar.YEAR);
		// ���ռ���ԭ����day�����������month�裻Ȼ��month�����������year�裻���year�����
		if (day < 0) {
			month -= 1;
			days1.add(Calendar.MONTH, -1);// �õ���һ���£������õ��ϸ��µ�������
			day = day + days1.getActualMaximum(Calendar.DAY_OF_MONTH);
		}
		if (month < 0) {
			month = (month + 12) % 12;
			year--;
		}
		// ���ֿ����ԣ� �ֱ� ���꼸���¼��죬�����¼��죬����
		if (year == 0 && month == 0) {
			age = Double.valueOf(day);
		} else if (year == 0 && month != 0) {
			age = Double.valueOf(month + "." + day);
		} 
		return age;
	}
	
	/**  
     * ����2��ʱ��֮����ٸ��¼���
     * @param startDateStr  
     * @param endDateStr  
     * @return  
     */   

    public static String remainDateToString(String startDateStr, String endDateStr){   
    	 Calendar calS=Calendar.getInstance(); 
    	 Pattern   p   =   Pattern.compile("\\d{4}-\\d{2}-\\d{2}");//����������ʽ  
    	Date startDate = null;   
        Date endDate= null;   
        try {   
            startDate = new SimpleDateFormat("yyyy-MM-dd").parse(startDateStr);   
            endDate = new SimpleDateFormat("yyyy-MM-dd").parse(endDateStr);   
        } catch (Exception e) {   
            e.printStackTrace();   
            return "";   
        }   
        calS.setTime(startDate);   
        int startY = calS.get(Calendar.YEAR);   
        int startM = calS.get(Calendar.MONTH);   
        int startD = calS.get(Calendar.DATE);   
        int startDayOfMonth = calS.getActualMaximum(Calendar.DAY_OF_MONTH);   
        
        calS.setTime(endDate);   
        int endY = calS.get(Calendar.YEAR);   
        int endM = calS.get(Calendar.MONTH); 
        int endD = calS.get(Calendar.DATE)+1;   
        int endDayOfMonth = calS.getActualMaximum(Calendar.DAY_OF_MONTH);   
           
        StringBuilder sBuilder = new StringBuilder();   
        if (endDate.compareTo(startDate)<0) {   
            return sBuilder.append("����").toString();   
        }   
        int lday = endD-startD;  
        if (lday<0) {   
            endM = endM -1;   
            lday = startDayOfMonth+ lday;   
        }   
      
        if (lday == endDayOfMonth) {   
            endM = endM+1;   
            lday =0;   
        }   
        int mos = (endY - startY)*12 + (endM- startM);   
        int lyear = mos/12;   
        int lmonth = mos%12;   
        if (lyear >0) {   
           //sBuilder.append(lyear+".");  
           lyear = lyear *12;
            
        }   
        if (lmonth > 0) {   
           // sBuilder.append(lmonth+".");   
            lyear = lyear+lmonth;
          
        } 
       
        if(lyear==0){
	        if (lday >0 ) {   
	            sBuilder.append(lday-1+".");   
	        }  
        }else{
           if(lday>0){
            sBuilder.append(lday+""); 
        }
        }
       return String.valueOf(lyear);  
        
    }  
    private static Calendar calS=Calendar.getInstance();   
    private static Pattern   p   =   Pattern.compile("\\d{4}-\\d{2}-\\d{2}");//����������ʽ   
   
    /**  
     * ����ʣ��ʱ��  
     * @param startDateStr  
     * @param endDateStr  
     * @return  
     */   
    public static String remainDateToString1(String startDateStr, String endDateStr){   
        java.util.Date startDate = null;   
        java.util.Date endDate= null;   
        try {   
            startDate = new SimpleDateFormat("yyyy-MM-dd").parse(startDateStr);   
            endDate = new SimpleDateFormat("yyyy-MM-dd").parse(endDateStr);   
        } catch (Exception e) {   
            e.printStackTrace();   
            return "";   
        }   
        calS.setTime(startDate);   
        int startY = calS.get(Calendar.YEAR);   
        int startM = calS.get(Calendar.MONTH);   
        int startD = calS.get(Calendar.DATE);   
        int startDayOfMonth = calS.getActualMaximum(Calendar.DAY_OF_MONTH);   
        
        calS.setTime(endDate);   
        int endY = calS.get(Calendar.YEAR);   
        int endM = calS.get(Calendar.MONTH); 
        //����2011-01-10��2011-01-10����Ϊ����Ϊһ��   
        int endD = calS.get(Calendar.DATE)+1;   
        int endDayOfMonth = calS.getActualMaximum(Calendar.DAY_OF_MONTH);   
           
        StringBuilder sBuilder = new StringBuilder();   
        if (endDate.compareTo(startDate)<0) {   
            return sBuilder.append("����").toString();   
        }   
        int lday = endD-startD;  
        if (lday<0) {   
            endM = endM -1;   
            lday = startDayOfMonth+ lday;   
        }   
        //�����������⣬�磺2011-01-01 �� 2013-12-31  2��11����31��     ʵ���Ͼ���3��   
        if (lday == endDayOfMonth) {   
            endM = endM+1;   
            lday =0;   
        }   
        int mos = (endY - startY)*12 + (endM- startM);   
        int lyear = mos/12;   
        int lmonth = mos%12;   
        if (lyear >0) {   
            sBuilder.append(lyear+"��");   
        }   
        if (lmonth > 0) {   
            sBuilder.append(lmonth+"����");   
        } 
       
        //������С��һ���ʱ��
        if(lyear==0){
         //sBuilder.append(lday+"��");   
	        if (lday >0 ) {   
	            sBuilder.append(lday-1+"��");   
	        }  
        }else{
           if(lday>0){
            sBuilder.append(lday+"��"); 
        }
        }
        return sBuilder.toString();   
    }   
}
